package com.aiyige.page.publish.inputprice;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.aiyige.R;
import com.aiyige.arouter.config.ARouterConfig;
import com.aiyige.utils.ToastX;
import com.aiyige.utils.widget.NumberUtil;
import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

@Route(path = ARouterConfig.InputFloatPricePage)
public class InputFloatPricePage extends AppCompatActivity {
    public static final String EXTRA_KEY_INPUT_PRICE = "com.aiyige.extra.EXTRA_KEY_INPUT_PRICE";
    @BindView(R.id.titleBackBtn)
    ImageView titleBackBtn;
    @BindView(R.id.titleTv)
    TextView titleTv;
    @BindView(R.id.titleActionBtn)
    Button titleActionBtn;
    @BindView(R.id.inputEt)
    EditText inputEt;
    @Autowired
    String text="";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.page_input_float_price);
        ButterKnife.bind(this);
        ARouter.getInstance().inject(this);
        titleTv.setText(R.string.price);
        titleActionBtn.setText(R.string.finish);
        inputEt.setText(text);
        inputEt.setSelection(inputEt.getText().length());
    }

    @OnClick({R.id.titleBackBtn, R.id.titleActionBtn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.titleBackBtn:
                setResult(RESULT_CANCELED);
                finish();
                break;
            case R.id.titleActionBtn:
                if (!TextUtils.isEmpty(inputEt.getText().toString().trim())) {
                    String priceStr = inputEt.getText().toString();
                    if (!NumberUtil.isNumberWithTwoDecimal(priceStr)) {
                        ToastX.show(R.string.reserve_two_decimal_fractions);
                        break;
                    }
                    try {
                        float price = Float.parseFloat(priceStr);//对输入的价格做一个简单的整形验证，为了灵活实际存储的还是字符串
                        Intent intent = new Intent();
                        intent.putExtra(EXTRA_KEY_INPUT_PRICE, String.valueOf(price));
                        setResult(RESULT_OK, intent);
                        finish();
                    } catch (Exception e) {
                        ToastX.show(R.string.invalid_price);
                    }
                }
                break;
        }
    }
}
